package com.nb.controller.user;

import com.nb.constant.MessageConstant;
import com.nb.dto.user.LoginDTO;
import com.nb.dto.user.RegisterDTO;
import com.nb.entity.User;
import com.nb.result.Result;
import com.nb.service.UserService;
import com.nb.utils.UrlUtil;
import com.nb.vo.user.UserLoginVO;
import com.nb.vo.user.UserRegisterVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



/**
 * 用户相关接口
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param registerDTO 注册信息
     * @return 用户信息
     */
    @PostMapping("/register")
    public Result<UserRegisterVO> register(@RequestBody RegisterDTO registerDTO) {
        log.info("用户注册：{}", registerDTO);
        
        // 调用业务层完成注册
        User user = userService.register(registerDTO);
        
        // 构建返回数据
        UserRegisterVO userRegisterVO = UserRegisterVO.builder()
                .id(user.getId())
                .account(user.getAccount())
                .build();
        
        return Result.success(MessageConstant.REGISTER_SUCCESS, userRegisterVO);
    }


    /**
     * 用户登录
     * @param loginDTO 登录信息
     * @return 登录结果(token + 用户信息)
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<UserLoginVO> login(@RequestBody LoginDTO loginDTO) {
        log.info("用户登录：{}", loginDTO);
        
        // 调用业务层完成登录
        UserLoginVO userLoginVO = userService.login(loginDTO);
        
        return Result.success(MessageConstant.LOGIN_SUCCESS, userLoginVO);
    }







}




